package com.yunquan.ohana.ui.history

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseFragment
import com.yunquan.ohana.databinding.FragmentHistoryBinding
import com.yunquan.ohana.ui.history.list.HistoryListFragment
import com.yunquan.ohana.utils.Utils

class HistoryFragment : BaseFragment<FragmentHistoryBinding, HistoryViewModel>() {


    companion object {
        fun newInstance(): HistoryFragment {
            return HistoryFragment()
        }
    }

    private val tabs = listOf("Sending", "Completed", "Failed", "Drafts")
    private val fragments = mutableListOf<HistoryListFragment>()

    override fun initContentView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): Int {
        return R.layout.fragment_history
    }

    override fun initVariableId(): Int {
        return BR.historyViewModel
    }

    override fun initData(savedInstanceState: Bundle?) {
        if (savedInstanceState != null) {
            fragments.addAll(childFragmentManager.fragments.filterIsInstance<HistoryListFragment>())
        }
        if (fragments.isEmpty()) {
            for (i in tabs.indices) {
                fragments.add(HistoryListFragment(tabs[i]))
            }
        }

        val adapter = PagerAdapter(requireActivity(), fragments)
        binding!!.vpContent.adapter = adapter
        TabLayoutMediator(binding!!.tabBar, binding!!.vpContent) { tab, position ->
            if (tabs.size > position) {
                tab.text = tabs[position]
                tab.view.setBackgroundColor(resources.getColor(R.color.transparent, null))
            }
        }.attach()
        for (i in binding!!.tabBar.tabCount - 1 downTo 0) {
            val tab = binding!!.tabBar.getTabAt(i)
            val layoutParams = tab!!.view.layoutParams as ViewGroup.MarginLayoutParams
            layoutParams.marginStart = Utils.instance.dp2px(16f)
            tab.view.layoutParams = layoutParams
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel!!.ui.observe(viewLifecycleOwner) { tyep ->
            when (tyep) {
                0 -> {
                    binding!!.vpContent.post {
                        binding!!.vpContent.setCurrentItem(0, false)
                    }
                }
            }
        }
    }
}

class PagerAdapter(activity: FragmentActivity, private val tabs: List<HistoryListFragment>) : FragmentStateAdapter(activity) {
    override fun getItemCount(): Int {
        return tabs.size
    }

    override fun createFragment(position: Int): Fragment {
        return tabs[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun containsItem(itemId: Long): Boolean {
        return itemId in tabs.indices
    }
}